package com.bullsora.kindlerss.repository;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyFactory;

import java.util.List;

/**
 * @author sorin.buliarca@iquesint.com
 */
public class GenericOperations {

  private ObjectifyFactory objectifyFactory;

  private Objectify objectify;

  public void delete(Iterable<?> keysOrEntities) {
    objectify.delete(keysOrEntities);
  }

  public <T> T find(Key<? extends T> key) {
    return objectify.find(key);
  }

  public <T> List<T> findAll(Class<T> entityClass) {
    return objectify.query(entityClass).list();
  }

  public void persist(Object entity) {
    objectify.put(entity);
  }

  public <T> T merge(T entity) {
    Key<T> key = objectify.put(entity);
    return objectify.get(key);
  }

  public void delete(Object entity) {
    objectify.delete(entity);
  }

  public ObjectifyFactory getObjectifyFactory() {
    return objectifyFactory;
  }

  public void setObjectifyFactory(ObjectifyFactory objectifyFactory) {
    this.objectifyFactory = objectifyFactory;
    objectify = objectifyFactory.begin();
  }

  public Objectify getObjectify() {
    return objectify;
  }
}
